﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace OT.Common
{
    public class BinarySeries
    {
        public static byte[] SeriesToByte(object pobj)
        {
            byte[] _Buffer = null;
            if (pobj == null)
                return _Buffer;

            IFormatter IFr = new BinaryFormatter();
            MemoryStream ms = new MemoryStream();
            IFr.Serialize(ms, pobj);
            ms.Position = 0;
            _Buffer = new byte[ms.Length];
            ms.Read(_Buffer, 0, _Buffer.Length);
            ms.Close();
            ms.Dispose();

            return _Buffer;
        }

        public static object SeriesToObject(byte[] pBuffer)
        {
            object _obj = null;
            if (pBuffer == null)
                return _obj;

            IFormatter IFr = new BinaryFormatter();
            MemoryStream ms = new MemoryStream();
            ms.Write(pBuffer, 0, pBuffer.Length);
            ms.Position = 0;
            _obj = IFr.Deserialize(ms);
            ms.Close();
            ms.Dispose();

            return _obj;
        }

        public static int SeriesObjectSize(object pobj)
        {
            byte[] _Buffer = SeriesToByte(pobj);
            if (_Buffer != null)
                return _Buffer.Length;

            return 0;
        }
    }
}
